package com.kaili.basic.service.impl;

/**
 * Created by xxx on 2019-03-07.
 */

import com.baomidou.mybatisplus.plugins.Page;
import com.kaili.basic.mapper.*;
import com.kaili.basic.pojo.dto.BusinessOperationDeleteDto;
import com.kaili.basic.pojo.dto.WarehousingReturnAddDto;
import com.kaili.basic.pojo.dto.WarehousingReturnProductDto;
import com.kaili.basic.pojo.dto.WarehousingReturnQueryDto;
import com.kaili.basic.pojo.entity.*;
import com.kaili.basic.service.WarehousingApplicationService;
import com.kaili.basic.service.WarehousingReturnService;
import com.kaili.common.util.LoginDetailUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author xxx
 * @Copyright: Copyright (c) 2017
 * @Company: 杭州凯立通信有限公司
 */
@Service
public class WarehousingReturnServiceImpl implements WarehousingReturnService {
    @Autowired
    WarehousingReturnMapper warehousingReturnMapper;
    @Autowired
    WarehousingReturnProductMapper warehousingReturnProductMapper;
    @Autowired
    SupplierMapper supplierMapper;
    @Autowired
    WarehousingApplicationService warehousingApplicationService;
    @Autowired
    WarehousingProductMapper warehousingProductMapper;
    @Autowired
    WarehousingApplicationMapper warehousingApplicationMapper;

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public int addWarehousingReturn(WarehousingReturnAddDto warehousingReturnAddDto) {
        WarehousingReturn warehousingReturn = new WarehousingReturn();
        BeanUtils.copyProperties(warehousingReturnAddDto, warehousingReturn);
        Long returnId = warehousingReturnMapper.getMaxReturnId();
        returnId = returnId + 1L;
        warehousingReturn.setReturnOdd(StringUtils.leftPad(returnId.toString(), 6, "0"));
        warehousingReturn.setPreparedBy(LoginDetailUtils.getCurrent().getUsername());
        warehousingReturn.setCreateBy(LoginDetailUtils.getCurrentEmployeeId().longValue());
        warehousingReturn.setReturnId(returnId);
        Supplier supplier=supplierMapper.querySupplierById(warehousingReturn.getSupplierId());
        if(supplier!=null){
            warehousingReturn.setSupplierCode(supplier.getSupplierCode());
            warehousingReturn.setSupplierName(supplier.getSupplierName());
        }
        warehousingReturnMapper.addWarehousingReturn(warehousingReturn);
        int order=1;
        for (WarehousingReturnProductDto warehousingReturnProductDto : warehousingReturnAddDto.getWarehousingReturnProductList()) {
            WarehousingReturnProduct warehousingReturnProduct=new WarehousingReturnProduct();
            BeanUtils.copyProperties(warehousingReturnProductDto, warehousingReturnProduct);
            warehousingReturnProduct.setReturnId(returnId);
            warehousingReturnProduct.setCreateBy(warehousingReturn.getCreateBy());
            warehousingReturnProduct.setReturnOrder(order);
            warehousingReturnProductMapper.addWarehousingReturnProduct(warehousingReturnProduct);
            order++;
        }
        //修改入库申请以及入库产品信息(数量，重量，体积等)
        warehousingApplicationService.updateOrderWarehousingReturn(warehousingReturnAddDto.getWarehousingReturnProductList(),warehousingReturnAddDto.getRequestCode());
        return 1;
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public int deleteWarehousingReturn(WarehousingReturn warehousingReturn) {
        warehousingReturnMapper.deleteWarehousingReturn(warehousingReturn);
        WarehousingReturnProduct warehousingReturnProduct =new WarehousingReturnProduct();
        warehousingReturnProduct.setReturnId(warehousingReturn.getReturnId());
        warehousingReturnProduct.setModifyBy(warehousingReturn.getModifyBy());
        warehousingReturnProductMapper.deleteWarehousingReturnProduct(warehousingReturnProduct);
        return 1;
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public int deleteWarehousingReturns(BusinessOperationDeleteDto deleteDto) {
        Long employeeId=LoginDetailUtils.getCurrentEmployeeId().longValue();
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("modifyBy",employeeId);
        if(deleteDto.getMainIds()!=null&&deleteDto.getMainIds().length>0){
            map.put("returnIds",deleteDto.getMainIds());
            warehousingReturnMapper.deleteWarehousingReturns(map);
            for(Long returnId:deleteDto.getMainIds()) {
                Long applyId = 0L;
                List<WarehousingReturnProduct> list = warehousingReturnProductMapper.getWarehousingReturnProductList(returnId);
                for (WarehousingReturnProduct warehousingReturnProductMy : list) {
                    //查询入库产品信息
                    WarehousingProduct warehousingProductMy = warehousingProductMapper.getWarehousingProductById(warehousingReturnProductMy.getInProductId());
                    //修改入库产品信息(数量，重量，体积等)
                    if (warehousingProductMy != null) {
                        applyId = warehousingProductMy.getApplyId();
                        WarehousingProduct warehousingProduct = new WarehousingProduct();
                        warehousingProduct.setId(warehousingReturnProductMy.getInProductId());
                        warehousingProduct.setModifyBy(employeeId);
                        warehousingProduct.setResidueQuantity(warehousingProductMy.getResidueQuantity() + warehousingReturnProductMy.getQuantity());
                        warehousingProduct.setResidueWeight(warehousingProductMy.getResidueWeight() + warehousingReturnProductMy.getWeight());
                        warehousingProduct.setResidueVolume(warehousingProductMy.getResidueVolume() + warehousingReturnProductMy.getVolume());
                        warehousingProduct.setResidueTray(warehousingProductMy.getResidueTray() + warehousingReturnProductMy.getTray());
                        warehousingProduct.setSalesReturnQuantity(warehousingProductMy.getSalesReturnQuantity() - warehousingReturnProductMy.getQuantity());
                        warehousingProductMapper.updateWarehousingProduct(warehousingProduct);
                    }
                }
                //查询子项的主表下总入库数量和总数量的差来判断入库的状态
                WarehousingApplication warehousingApplication = new WarehousingApplication();
                warehousingApplication.setApplyId(applyId);
                warehousingApplication.setModifyBy(employeeId);
                Long sumReturnQuantity = warehousingApplicationMapper.getSumReturnQuantity(applyId);
                if (sumReturnQuantity == null) {
                    warehousingApplication.setSalesReturnStatus(2);
                } else {
                    if (sumReturnQuantity.longValue() > 0L) {
                        warehousingApplication.setSalesReturnStatus(1);
                    } else {
                        warehousingApplication.setSalesReturnStatus(2);
                    }
                }
                warehousingApplicationMapper.updateWarehousingApplication(warehousingApplication);
            }
        }
        if(deleteDto.getSonIds()!=null&&deleteDto.getSonIds().length>0){
            map.put("ids",deleteDto.getSonIds());
            warehousingReturnProductMapper.deleteWarehousingReturnProductList(map);
            for(Long id:deleteDto.getSonIds()) {
                WarehousingReturnProduct warehousingReturnProduct = warehousingReturnProductMapper.getWarehousingReturnProductByIdAll(id);
                //如果批量删除中有当前子项主表的id则不需要回退入库信息的数量，体积等信息，以免多回退数量，体积等
                WarehousingReturn warehousingReturn = warehousingReturnMapper.queryWarehousingReturnById(warehousingReturnProduct.getReturnId());
                if (warehousingReturn != null) {
                    Long applyId = 0L;
                    WarehousingProduct warehousingProductMy = warehousingProductMapper.getWarehousingProductById(warehousingReturnProduct.getInProductId());
                    if (warehousingProductMy != null) {
                        applyId = warehousingProductMy.getApplyId();
                        WarehousingProduct warehousingProduct = new WarehousingProduct();
                        warehousingProduct.setId(warehousingReturnProduct.getInProductId());
                        warehousingProduct.setModifyBy(employeeId);
                        warehousingProduct.setResidueQuantity(warehousingProductMy.getResidueQuantity() + warehousingReturnProduct.getQuantity());
                        warehousingProduct.setResidueWeight(warehousingProductMy.getResidueWeight() + warehousingReturnProduct.getWeight());
                        warehousingProduct.setResidueVolume(warehousingProductMy.getResidueVolume() + warehousingReturnProduct.getVolume());
                        warehousingProduct.setResidueTray(warehousingProductMy.getResidueTray() + warehousingReturnProduct.getTray());
                        warehousingProduct.setSalesReturnQuantity(warehousingProductMy.getSalesReturnQuantity() - warehousingReturnProduct.getQuantity());
                        warehousingProductMapper.updateWarehousingProduct(warehousingProduct);
                    }
                    //修改入库申请信息的退货状态，总的退货数量为0则没有退货
                    WarehousingApplication warehousingApplication = new WarehousingApplication();
                    warehousingApplication.setApplyId(applyId);
                    warehousingApplication.setModifyBy(employeeId);
                    Long sumReturnQuantity = warehousingApplicationMapper.getSumReturnQuantity(applyId);
                    if (sumReturnQuantity == null) {
                        warehousingApplication.setSalesReturnStatus(2);
                    } else {
                        if (sumReturnQuantity.longValue() > 0L) {
                            warehousingApplication.setSalesReturnStatus(1);
                        } else {
                            warehousingApplication.setSalesReturnStatus(2);
                        }
                    }
                    warehousingApplicationMapper.updateWarehousingApplication(warehousingApplication);
                }
            }
        }
        return 1;
    }

    @Override
    public Page<WarehousingReturn> queryWarehousingReturn(Page<WarehousingReturn> page, WarehousingReturnQueryDto warehousingReturn) {
        List<WarehousingReturn> warehousingReturns = warehousingReturnMapper.queryWarehousingReturn(page,warehousingReturn);
        return page.setRecords(warehousingReturns);
    }

    @Override
    public WarehousingReturn queryWarehousingReturnById(Long returnId) {
        return warehousingReturnMapper.queryWarehousingReturnById(returnId);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public int updateAffirmOrCancel(WarehousingReturn warehousingReturn) {
        WarehousingReturn warehousingReturnMy=warehousingReturnMapper.queryWarehousingReturnById(warehousingReturn.getReturnId());
        if(warehousingReturnMy!=null&&warehousingReturnMy.getOrderStatus().intValue()==1) {
            warehousingReturnMapper.updateWarehousingReturn(warehousingReturn);
            WarehousingReturnProduct warehousingReturnProduct = new WarehousingReturnProduct();
            warehousingReturnProduct.setReturnId(warehousingReturn.getReturnId());
            warehousingReturnProduct.setModifyBy(warehousingReturn.getModifyBy());
            warehousingReturnProduct.setOrderStatus(warehousingReturn.getOrderStatus());
            warehousingReturnProductMapper.updateConfirmToCancel(warehousingReturnProduct);
            Long applyId = 0L;
            List<WarehousingReturnProduct> list = warehousingReturnProductMapper.getWarehousingReturnProductList(warehousingReturn.getReturnId());
            for (WarehousingReturnProduct warehousingReturnProductMy : list) {
                WarehousingProduct warehousingProductMy = warehousingProductMapper.getWarehousingProductById(warehousingReturnProductMy.getInProductId());
                if (warehousingProductMy != null) {
                    applyId = warehousingProductMy.getApplyId();
                    WarehousingProduct warehousingProduct = new WarehousingProduct();
                    warehousingProduct.setId(warehousingReturnProductMy.getInProductId());
                    warehousingProduct.setModifyBy(warehousingReturn.getModifyBy());
                    if (warehousingReturn.getOrderStatus().intValue() == 2) {
                        //订单确认的操作
                        warehousingProduct.setSalesReturnQuantity(warehousingProductMy.getSalesReturnQuantity() - warehousingReturnProductMy.getQuantity());
                        warehousingProduct.setActualQuantityReturned(warehousingProductMy.getActualQuantityReturned() + warehousingReturnProductMy.getQuantity());
                    } else {
                        //订单取消的操作，回退数量，体积等
                        warehousingProduct.setResidueQuantity(warehousingProductMy.getResidueQuantity() + warehousingReturnProductMy.getQuantity());
                        warehousingProduct.setResidueWeight(warehousingProductMy.getResidueWeight() + warehousingReturnProductMy.getWeight());
                        warehousingProduct.setResidueVolume(warehousingProductMy.getResidueVolume() + warehousingReturnProductMy.getVolume());
                        warehousingProduct.setResidueTray(warehousingProductMy.getResidueTray() + warehousingReturnProductMy.getTray());
                        warehousingProduct.setSalesReturnQuantity(warehousingProductMy.getSalesReturnQuantity() - warehousingReturnProductMy.getQuantity());
                    }
                    warehousingProductMapper.updateWarehousingProduct(warehousingProduct);
                }
            }
            //修改入库申请信息的退货状态，总的退货数量为0则没有退货
            WarehousingApplication warehousingApplication = new WarehousingApplication();
            warehousingApplication.setApplyId(applyId);
            warehousingApplication.setModifyBy(warehousingReturn.getModifyBy());
            Long sumReturnQuantity = warehousingApplicationMapper.getSumReturnQuantity(applyId);
            if (sumReturnQuantity == null) {
                warehousingApplication.setSalesReturnStatus(2);
            } else {
                if (sumReturnQuantity.longValue() > 0L) {
                    warehousingApplication.setSalesReturnStatus(1);
                } else {
                    warehousingApplication.setSalesReturnStatus(2);
                }
            }
            warehousingApplicationMapper.updateWarehousingApplication(warehousingApplication);
        }
        return 1;
    }

    @Override
    public Long getMaxReturnId() {
        return warehousingReturnMapper.getMaxReturnId();
    }

    @Override
    public List<WarehousingReturnExport> queryWarehousingReturnExport(WarehousingReturnQueryDto warehousingReturnQueryDto) {
        return warehousingReturnMapper.queryWarehousingReturnExport(warehousingReturnQueryDto);
    }
}
